/**
 * @ClassName: AboutFragment
 * @Description: 关于页面
 * @author Mengting Lu
 * @date 2015年7月31日 下午1:58:35
 */

package com.shanghaionstar.discover.charge;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.weight.CustomButton;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.DiscoverActivity;
import com.shanghaionstar.carmanage.CarManageActivity;
import com.shanghaionstar.discover.charge.adapter.ChargeModeAdapter;
import com.shanghaionstar.discover.charge.iview.IChargeModeView;
import com.shanghaionstar.discover.charge.model.ChargeModeItem;
import com.shanghaionstar.discover.charge.presenter.ChargeModePresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.weight.TopLayout;

import java.util.List;

/**
 * @author Kai Zhang
 * @ClassName: ChargeModeFragment
 * @Description: 充电模式
 * @date 2015年12月7日
 */

public class ChargeModeFragment extends BaseFragment implements OnClickListener,IChargeModeView {
	@FindViewById
	private TopLayout top_layout;
	@FindViewById
	private ListView charge_modes_lv;
	@FindViewById
	private CustomButton btn_save;
	@FindViewById
	private ProgressLayout cm_progress;
	@FindViewById
	private LinearLayout charge_modes_time_layout;
	@FindViewById
	private RelativeLayout layout_background;

	private ChargeModeAdapter adapter;
	private ChargeModePresenter presenter;


	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (isFirstLoad()) {
			presenter = new ChargeModePresenter(((BaseActivity) getActivity()).getScheduler());
			presenter.setView(this);
			presenter.initList();

//			top_layout.setLeftButtonClickListener(new OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					back();
//				}
//			});
			if(getActivity() instanceof DiscoverActivity) {
//				top_layout.setRightTextBg(R.drawable.onstar_personal_btn);
				top_layout.setOnRightTextClickLstener(getString(R.string.option_notify_set), new OnClickListener() {
					@Override
					public void onClick(View v) {
						presenter.onRightClick();
					}
				});
			}else if(getActivity() instanceof CarManageActivity){
				layout_background.setBackgroundColor(Color.WHITE);
				top_layout.setVisibility(View.GONE);
			}
//			top_layout.setRightButtonClickListener(R.string.option_notify_set, R.drawable.onstar_personal_btn,
//					new OnClickListener() {
//						@Override
//						public void onClick(View arg0) {
//							presenter.onRightClick();
//						}
//					});
			if(ApplicationUtil.getInstance().isLogin())
				presenter.requestChargeMode();
		}

	}




	/**
	 * (non-Javadoc)
	 * <p>
	 * Title: onClick
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 *
	 * @param v
	 * @see OnClickListener#onClick(View)
	 */
	@OnClick({ R.id.btn_save, R.id.charge_modes_time_layout })
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_save:
			presenter.save();
			break;
		case R.id.charge_modes_time_layout:
			presenter.switchToChargeTime();
			break;
		default:
			break;
		}
	}


	/**
	 * (non-Javadoc)
	 * <p>
	 * Title: getLayoutId
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 *
	 * @return
	 * @see BaseFragment#getLayoutId()
	 */
	@Override
	public int getLayoutId() {
		return R.layout.discover_charge_mode;
	}

	private void enableButton(boolean enable) {
		charge_modes_time_layout.setEnabled(enable);
		charge_modes_lv.setEnabled(enable);
		btn_save.setEnabled(enable);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		top_layout = null;
		presenter.finish();
	}

	@Override
	public void setLoading(boolean isLoading) {
		cm_progress.setVisibility(isLoading?View.VISIBLE:View.GONE);
		enableButton(!isLoading);
	}

	@Override
	public void onResponseError(OnStarException e) {
		setLoading(false);
		AlertDialogUtil.showDialogByOnstarException(onActivity(), e);
	}

	@Override
	public void onReponseSuccess(Class<?> object) {

	}

	@Override
	public Activity onActivity() {
		return getActivity();
	}

	@Override
	public void initView(List<ChargeModeItem> list) {
		adapter = new ChargeModeAdapter(list, getActivity());
		charge_modes_lv.setAdapter(adapter);
		charge_modes_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				presenter.onItemClick(position);
				adapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	public void notifyChange() {
		adapter.notifyDataSetChanged();
	}


}
